package com.chuanyang.driverh.mainstart.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseListAdapter;
import com.chuanyang.driverh.mainhome.BidOrderActivity;
import com.chuanyang.driverh.mainhome.OrderDetailRceiveActivity;
import com.chuanyang.driverh.responseresult.InvoceOrderData;
import com.chuanyang.driverh.responseresult.OrderDatailResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class IncvocePriceAdapterLeader extends BaseListAdapter<InvoceOrderData.DataBean> {//首页竞价大厅的adapter
    private ViewHolder viewHolder;
    private Activity activity;
    public IncvocePriceAdapterLeader(Activity activity) {
        super(activity);//此处不传入集合，则默认集合长度为0，在setdataresult（）方法中，添加数据并刷新
        this.activity=activity;
    }

    @Override
    public View getZkView(final int position, View convertView, final ViewGroup parent) {
        if (convertView == null) {
            convertView = getInflater().inflate(R.layout.home_item_invocepricehall_leader, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            viewHolder.tv_city_start = (TextView) convertView.findViewById(R.id.tv_city_start);
            viewHolder.tv_city_end = (TextView) convertView.findViewById(R.id.tv_city_end);
            viewHolder.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
            viewHolder.tv_goods_name = (TextView) convertView.findViewById(R.id.tv_goods_name);
            viewHolder.tv_weight = (TextView) convertView.findViewById(R.id.tv_weight);
            viewHolder.tv_load = (TextView) convertView.findViewById(R.id.tv_load);
            viewHolder.tv_unload = (TextView) convertView.findViewById(R.id.tv_unload);
            viewHolder.tv_time_load = (TextView) convertView.findViewById(R.id.tv_time_load);
            viewHolder.tv_time_unload = (TextView) convertView.findViewById(R.id.tv_time_unload);
            viewHolder.tv_bid_order = (TextView) convertView.findViewById(R.id.tv_bid_order);
            viewHolder.tv_person_num = (TextView) convertView.findViewById(R.id.tv_person_num);
            viewHolder.iv_car_or_ship= (ImageView) convertView.findViewById(R.id.iv_car_or_ship);
            viewHolder.ll_div= (LinearLayout) convertView.findViewById(R.id.ll_div);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();

        }
       final InvoceOrderData.DataBean data = getItem(position);


//        viewHolder.tv_time.setText(TimeUtils.getMiniter(data.getExpiredTime()));
        viewHolder.tv_time.setText(TimeUtils.getLeftTime(data.getNow()));


        viewHolder.tv_goods_name.setText(data.getGoodsNames());
        viewHolder.tv_city_start.setText(StringUtil.getSimpleMunicipality(data.getStartAddr()));
        viewHolder.tv_city_end.setText(StringUtil.getSimpleMunicipality(data.getTargetAddr()));
        viewHolder.tv_num.setText(data.getGoodsNum());
        viewHolder.tv_weight.setText(data.getGoodsWeight());
        viewHolder.tv_bid_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               /* InvoceOrderData.DataBean order = getItem(position);
                if ("0".equals(TimeUtils.getMiniter(order.getExpiredTime()))){
                    PrintUtil.toast(activity,"该订单已过期");
                    EventBusUtil.postEvent(new RefleshDriverInfoEvent(true));
                    return;
                }*/


               /* Intent intent = new Intent(activity, NewInvoceOrderInfoActivity.class);
                intent.putExtra("orderId", data.getOrderId());
                intent.putExtra("order", data);
                activity.startActivity(intent);*/
              /*  Intent intent = new Intent(activity, BidOrderActivity.class);
                intent.putExtra("orderId", data.getOrderId());
                intent.putExtra("order", data);
                activity.startActivity(intent);*/
                requsetData(data.getOrderId().intValue()+"");

            }
        });

        viewHolder.ll_div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, OrderDetailRceiveActivity.class);
                intent.putExtra("data", data.getOrderId()+"");
                intent.putExtra("type", "1");
                intent.putExtra("minPrice", data.getMinPrice()+"");
                activity.startActivity(intent);
            }
        });

        viewHolder.tv_person_num.setText(data.getBidCount());

        if (data.getTransPortWay() == 1)//车
        {
            viewHolder.tv_load.setText("提");
            viewHolder.tv_unload.setText("卸");
            viewHolder.tv_load.setPadding(0,0,0,0);
            viewHolder.tv_unload.setPadding(0,0,0,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_point_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_point_unload);

            viewHolder.tv_time_load.setText(data.getOrder_startime());
            viewHolder.tv_time_unload.setText(data.getOrder_endtime());
            viewHolder.iv_car_or_ship.setImageResource(R.mipmap.main_bid_vehicle);

        } else if (data.getTransPortWay() == 2)//船
        {
            viewHolder.tv_load.setText("装卸天数");
            viewHolder.tv_unload.setText("受载日期");
            viewHolder.tv_load.setPadding(6,0,6,0);
            viewHolder.tv_unload.setPadding(6,0,6,0);
            viewHolder.tv_load.setBackgroundResource(R.drawable.tv_shape_round_corner_load);
            viewHolder.tv_unload.setBackgroundResource(R.drawable.tv_shape_round_corner_unload);

            viewHolder.tv_time_load.setText(data.getStevedoreDay());
            viewHolder.tv_time_unload.setText(data.getLoadDate());
            viewHolder.iv_car_or_ship.setImageResource(R.mipmap.main_bid_ship);
        }


        return convertView;
    }

    class ViewHolder {
        TextView tv_time;
        TextView tv_goods_name;
        TextView tv_city_start;
        TextView tv_city_end;
        TextView tv_num;
        TextView tv_weight;
        TextView tv_load;
        TextView tv_unload;
        TextView tv_time_load;
        TextView tv_time_unload;
        TextView tv_bid_order;
        TextView tv_person_num;
        ImageView iv_car_or_ship;
        LinearLayout ll_div;

    }



    private void requsetData(String orderId) {
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());
        params.put("orderId", orderId);
        MyHttpUtil.selectOrderMsgById(activity,params, new MyCallback<OrderDatailResult>(activity,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activity, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(OrderDatailResult result, int id) {

                if (result.getCode() == ConstUtil.OK ) {
                    activity.startActivity(new Intent(activity,BidOrderActivity.class).putExtra("data",result.getData()));

                }else{
                    PrintUtil.toast(activity,result.getMsg());
                }
            }
        });

    }
}
